<?php
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
//二次确认页面
$return=array();
$basic=$this->plugin_setting('basic'.$_SESSION['onemerchid']);
if($_RQ['op']=='main'){
    set_html('title','确认就诊');
    $post['merchid'] = $_RQ['merchid'];
    $post['store_id'] = $_RQ['store_id'];
    if($_RQ['postdata']){
		$postdata=get_postdata();
	    if($postdata['merchid']){
    	    $setting = $this->plugin_setting('basic'.$postdata['merchid']);
    	}
    	//营业时间内可以进行签到
    	if($setting['onlinetime']['start']>date('H:i') || $setting['onlinetime']['end']<date('H:i')){
    	    exi('不在营业时间内无法签到,请营业在进行操作','error');
    	}
		//查询用户档案信息
		$daan = pdo_get('jy_yiliao_body',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'merchid'=>$postdata['merchid']));
		$userinfo = pdo_get('jy_yiliao_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'bodyid'=>$daan['id']));
	
		$qdlinelog = pdo_count('jy_yiliao_qdlinelog',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$postdata['merchid'],'store_id'=>$postdata['store_id'],'createtime'=>date('Y-m-d'),'mid'=>$_SESSION['uid']));
		$user_ordercount = pdo_count('jy_yiliao_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(1,2,3),'mid'=>$_SESSION['uid'],'day'=>date('Y-m-d'),'merchid'=>$postdata['merchid']));
		
		if($daan && $qdlinelog){
		    exi('今天已经进行签到就诊了,不能进行操作了!!','error');
		}
		//查询用户是否有预约订单,没有执行优先线上预约挂号患者
		$insert = array(
		  'ordersn'=>'QD'.date('YmdHis').rand(1,9999),
		  'uniacid'=>$_SESSION['uniacid'],
		  'mid'=>$_SESSION['uid'],
		  'merchid'=>$postdata['merchid'],
		  'store_id'=>$postdata['store_id'],
		  'worker_id'=>$postdata['worker_id'],
		  'uname'=>$userinfo['name'],
		  'uphone'=>$userinfo['mobile'],
		  'createtime'=>date('Y-m-d')
		);
       
		if($user_ordercount>0){
		     $dates = date("H:i");
		     $order_count = pdo_count('jy_yiliao_lesson_log',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(1,2,3),'daydata'=>date('Y-m-d'),'merchid'=>$postdata['merchid'],'ontimestart <='=>$dates));
		    //线上预约
    		$pdlog = pdo_count('jy_yiliao_qdlinelog',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$postdata['store_id'],'createtime'=>date('Y-m-d'),'merchid'=>$postdata['merchid'],'source'=>1));
    		if($order_count==0){
    		     $order_count=1;
    		}
    		if($pdlog>0){
    		    $order_count = $pdlog + 1;
    		}
		    //读取订单排队号
		    //$user_order = pdo_get('jy_yiliao_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(1,2,3),'mid'=>$_SESSION['uid'],'day'=>date('Y-m-d'),'merchid'=>$postdata['merchid']));
		    //$pnumber = 'B0'.$user_order['pnumber'];
		    ///$insert['pnumber'] = $pnumber;
		    //等待人数
		    //$insert['lastnum'] = $user_order['pnumber'] - 1;
		    
		    $insert['pnumber'] = 'B0'.$order_count;
		    //等待人数
		    $insert['lastnum'] = $order_count - 1;
		    
		    $insert['source'] = 1;
		    pdo_insert('jy_yiliao_qdlinelog',$insert);
		    
		    $logid = pdo_insertid();
		  
		    //打印小票
	        model('jy_weishop/printmsg')->sign_print(1,$userinfo['id'],$logid,11,$postdata['merchid']);
	        $orderlog = pdo_get('jy_yiliao_lesson_log',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(1,2,3),'daydata'=>date('Y-m-d'),'merchid'=>$postdata['merchid'],'mid'=>$_SESSION['uid']));
	        pdo_update('jy_yiliao_lesson_log',array('status'=>2),array('id'=>$orderlog['id'],'uniacid'=>$_SESSION['uniacid']));
	        
		}else{
		   
		   //线下到店
		   $pdlog = pdo_count('jy_yiliao_qdlinelog',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$postdata['store_id'],'createtime'=>date('Y-m-d'),'merchid'=>$postdata['merchid'],'source'=>2));
		   if($order_count==0 && $pdlog==0){
		       $order_count=1;
		   }
		 
		   if($pdlog>0){
		       $order_count = $pdlog + 1;
		   }
		   $insert['pnumber'] = 'A0'.$order_count;
		   //等待人数
		   $insert['lastnum'] = $order_count - 1;
		   pdo_insert('jy_yiliao_qdlinelog',$insert);
		   $logid = pdo_insertid();
		   //打印小票
	       model('jy_weishop/printmsg')->sign_print(1,$userinfo['id'],$logid,11,$postdata['merchid']);
		   //查询当日科室	
			$user=pdo_get('jy_yiliao_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),array('bodyid','name','birth','sex','mobile'));
			$start=date('H');
			$end=TIMESTAMP+3600;
			$end=date('H',$end);
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'bodyid'=>$user['bodyid'],
				'lesson_id'=>$lessonid,
				'day'=>date('Y-m-d'),
				'remark'=>'线下扫码问诊',
				'logsn'=>get_ordersn('jy_yiliao_lesson_log','logsn'),
				'ontime'=>ijson_encode(array('start'=>$start,'end'=>$end)),
				'createtime'=>TIMESTAMP,
				'storeid'=>$postdata['storeid']
			);
			$insert['username']=$user['name'];
			$insert['name']=$user['name'];
			$insert['sex']=$user['sex'];
			$birth=json_decode($user['birth'],true);
			$year=$birth['year'];
			if($insert['bodyid']){
					$body=pdo_get('jy_yiliao_body',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['bodyid']),array('name','sex','agetime'));
					$insert['name']=$body['name'];
					$insert['sex']=$body['sex'];
					$year=date('Y',$body['agetime']);
			}
			$insert['age']=$year>0?date('Y')-$year:0;
			pdo_insert('jy_yiliao_lesson_log',$insert);
		}
    	$data=array(
			'userid'=>$userinfo['id'],
			'bodyid'=>$daan['bodyid'],
			'mid'=>$daan['mid'],
    		'remark'=>'扫码到店，前来就诊！',
    	);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['status']=1;
		$data['createtime']=time();
		pdo_insert('jy_yiliao_binli',$data);
    	exi('确认中请等待...','success','jy_yiliao/hxcg//merchid='.$postdata['merchid']); 
	}
	$return['post']=$post;
	$return['sharedata']=array(
		'title'=>$basic['share_title'].'签到到店就诊',
		'desc'=>'线下到店面诊方便快捷',
		'link'=>custom_url('jy_yiliao/sign//merchid='.$_SESSION['onemerchid'].'&store_id='.$_RQ['store_id'],true),
		'imgUrl'=>ATTACHMENT_URL.$basic['share_image'],
	);
    exi($return);
}

?>